class Flyweight(object):
    def __init__(self, str):
        self.str = str
 
    def display(self):
        print("show the string: " + self.str)
 
class FlyweightFactory(object):
    def __init__(self):
        self.flyweights = {}
 
    def getFlyweight(self, obj):
        flyweight = self.flyweights.get(obj)
        if flyweight == None:
            flyweight = Flyweight(str(obj))
            self.flyweights[obj] = flyweight
 
        return flyweight
 
    def showFlyweights(self):
        for i in self.flyweights:
            self.flyweights[i].display()
        print(len(self.flyweights))
 
if __name__ == "__main__":
    flyweightfactory = FlyweightFactory()
    fly1 = flyweightfactory.getFlyweight("hello1")
    fly2 = flyweightfactory.getFlyweight("hello1")
    fly3 = flyweightfactory.getFlyweight("hello2")
    fly4 = flyweightfactory.getFlyweight("hello2")
    fly5 = flyweightfactory.getFlyweight("hello3")
 
    flyweightfactory.showFlyweights()
